﻿using Mengsk.ProxyDelivery.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Imaging;
using ZXing;
using ZXing.Rendering;
namespace Mengsk.ProxyDelivery.Print.Domain.PrintDataFormatters
{
    class PrintDataFormatterOther_Barcode : PrintDataFormatter
    {
        public override PrintTemplateItemTypes AcceptType
        {
            get { return PrintTemplateItemTypes.OTHER_BARCODE; }
        }

        public override object Format(PrintTemplate template, PrintTemplateItem item, Order order)
        {
            var writer = new ZXing.BarcodeWriter();
            writer.Format = (BarcodeFormat)(Enum.Parse(typeof(BarcodeFormat), item.Format));
            writer.Options = new ZXing.Common.EncodingOptions
            {
                Height = (int)item.Height,
                Width = (int)item.Width,
                Margin = 0,
                PureBarcode = item.Value1 == "是" ? false : true,
            };
            if (writer.Renderer is ZXing.Rendering.BitmapRenderer)
            {
                ((ZXing.Rendering.BitmapRenderer)writer.Renderer).TextFont = new System.Drawing.Font(item.FontName, (float)item.FontSize <= 0 ? 12 : (float)item.FontSize);
            }
            else if (writer.Renderer is ZXing.Rendering.WriteableBitmapRenderer)
            {
                ((ZXing.Rendering.WriteableBitmapRenderer)writer.Renderer).FontFamily = new System.Windows.Media.FontFamily(item.FontName);
                ((ZXing.Rendering.WriteableBitmapRenderer)writer.Renderer).FontSize = item.FontSize <= 0 ? 12 : item.FontSize;
            }
            var imageData = writer.Write(item.Value);
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(imageData.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight((int)imageData.Width, (int)imageData.Height));

        }
    }
}
